package service

import (
	"context"
	"gitee.com/byx_darwin/geo-rpc/kitex_gen/geo"
	"gitee.com/byx_darwin/micro-extend/pkg/errors/rpc"
	"github.com/bytedance/gopkg/cloud/metainfo"
	"github.com/pkg/errors"
)

type GeoRepo interface {
	Get(ctx context.Context, ip string, region geo.Region) (*geo.GeoObject, error)
}

func (s *Service) Get(ctx context.Context,
	info *geo.GetRequest) (*geo.GeoObject, error) {
	if err := s.CheckArgument(info); err != nil {
		return nil, err
	}
	ip, ok := metainfo.GetPersistentValue(ctx, "client_ip")
	if !ok {
		return nil, rpc.NewBizStatusError(rpc.ErrorTypeDataInvalid,
			errors.New("metainfo lack client_ip"))
	}
	return s.repo.Get(ctx, ip, info.Region)
}

func (s *Service) GetWithIP(ctx context.Context,
	info *geo.GetWithIPRequest) (*geo.GeoObject, error) {
	if err := s.CheckArgument(info); err != nil {
		return nil, err
	}
	return s.repo.Get(ctx, info.Ip, info.Region)
}
